home *** CD-ROM | disk | FTP | other *** search
- unit ClipFmtListU;
-
- {$ifdef Ver100} { Delphi 3.0x }
- {$define DelphiLessThan4}
- {$endif}
- {$ifdef Ver110} { C++ Builder 3.0x }
- {$define DelphiLessThan4}
- {$endif}
-
- interface
-
- uses
- ActiveX,
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ComCtrls, Menus, ExtCtrls;
-
- type
- TDataFormatListForm = class(TForm, IUnknown, IDropTarget)
- lstDragFmt: TListView;
- Splitter1: TSplitter;
- lstClipFmt: TListView;
- Timer: TTimer;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure TimerTimer(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure FormHide(Sender: TObject);
- private
- {$ifdef DelphiLessThan4}
- //IUnknown
- function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
- function _AddRef: Integer; stdcall;
- function _Release: Integer; stdcall;
- {$endif}
- //IDropTarget
- function DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
- pt: TPoint; var dwEffect: Longint): HResult; stdcall;
- function DragOver(grfKeyState: Longint; pt: TPoint;
- var dwEffect: Longint): HResult;
- {$ifndef DelphiLessThan4}reintroduce; {$endif}stdcall;
- function DragLeave: HResult; stdcall;
- function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
- var dwEffect: Longint): HResult; stdcall;
- //Other methods
- procedure ListFormats(List: TListItems; DataObj: IDataObject);
- end;
-
- var
- DataFormatListForm: TDataFormatListForm;
-
- implementation
-
- {$R *.DFM}
-
- uses
- ComObj;
-
- function ClipFormatToStr(Fmt: TClipFormat): String;
- var
- Buf: array[0..255] of Char;
- begin
- GetClipboardFormatName(Fmt, Buf, SizeOf(Buf));
- Result := String(Buf);
- if Result = '' then
- case Fmt of
- CF_TEXT: Result := 'CF_TEXT';
- CF_BITMAP: Result := 'CF_BITMAP';
- CF_METAFILEPICT: Result := 'CF_METAFILEPICT';
- CF_SYLK: Result := 'CF_SYLK';
- CF_DIF: Result := 'CF_DIF';
- CF_TIFF: Result := 'CF_TIFF';
- CF_OEMTEXT: Result := 'CF_OEMTEXT';
- CF_DIB: Result := 'CF_DIB';
- CF_PALETTE: Result := 'CF_PALETTE';
- CF_PENDATA: Result := 'CF_PENDATA';
- CF_RIFF: Result := 'CF_RIFF';
- CF_WAVE: Result := 'CF_WAVE';
- CF_UNICODETEXT: Result := 'CF_UNICODETEXT';
- CF_ENHMETAFILE: Result := 'CF_ENHMETAFILE';
- CF_HDROP: Result := 'CF_HDROP';
- CF_LOCALE: Result := 'CF_LOCALE';
- CF_OWNERDISPLAY: Result := 'CF_OWNERDISPLAY';
- CF_DSPTEXT: Result := 'CF_DSPTEXT';
- CF_DSPBITMAP: Result := 'CF_DSPBITMAP';
- CF_DSPMETAFILEPICT: Result := 'CF_DSPMETAFILEPICT';
- CF_DSPENHMETAFILE: Result := 'CF_DSPENHMETAFILE';
- else
- Result := 'Unknown clipboard format'
- end;
- Result := Format('%s (%d, $%1:x)', [Result, Fmt])
- end;
-
- function TyMedToStr(TyMed: Longint): String;
- var
- I: Integer;
- Started: Boolean;
- type
- TTyMed = record
- TyMed: Longint;
- Desc: String
- end;
- const
- TyMeds: array[0..6] of TTyMed = (
- (TyMed: TYMED_HGLOBAL; Desc: 'TYMED_HGLOBAL'),
- (TyMed: TYMED_FILE; Desc: 'TYMED_FILE'),
- (TyMed: TYMED_ISTREAM; Desc: 'TYMED_ISTREAM'),
- (TyMed: TYMED_ISTORAGE; Desc: 'TYMED_ISTORAGE'),
- (TyMed: TYMED_GDI; Desc: 'TYMED_GDI'),
- (TyMed: TYMED_MFPICT; Desc: 'TYMED_MFPICT'),
- (TyMed: TYMED_ENHMF; Desc: 'TYMED_ENHMF'));
- begin
- Result := '';
- Started := False;
- for I := Low(TyMeds) to High(TyMeds) do
- begin
- if TyMed and TyMeds[I].TyMed <> 0 then
- begin
- if Started then
- Result := Result + ' or ';
- Result := Result + TyMeds[I].Desc;
- Started := True
- end
- end;
- end;
-
- {$ifdef DelphiLessThan4}
- //IUnknown
- function TDataFormatListForm._AddRef: Integer;
- begin
- if VCLComObject = nil then
- Result := -1 // -1 indicates no reference counting is taking place
- else
- Result := IVCLComObject(VCLComObject)._AddRef;
- end;
-
- function TDataFormatListForm._Release: Integer;
- begin
- if VCLComObject = nil then
- Result := -1 // -1 indicates no reference counting is taking place
- else
- Result := IVCLComObject(VCLComObject)._AddRef;
- end;
-
- function TDataFormatListForm.QueryInterface(const IID: TGUID;
- out Obj): HResult;
- begin
- if VCLComObject = nil then
- begin
- if GetInterface(IID, Obj) then Result := S_OK
- else Result := E_NOINTERFACE
- end
- else
- Result := IVCLComObject(VCLComObject).QueryInterface(IID, Obj);
- end;
- {$endif}
-
- //IDropTarget
- function TDataFormatListForm.DragEnter(const dataObj: IDataObject;
- grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
- begin
- ListFormats(lstDragFmt.Items, dataObj);
- Result := S_OK
- end;
-
- function TDataFormatListForm.DragLeave: HResult;
- begin
- Result := S_OK
- end;
-
- function TDataFormatListForm.DragOver(grfKeyState: Integer; pt: TPoint;
- var dwEffect: Integer): HResult;
- begin
- Result := S_OK
- end;
-
- function TDataFormatListForm.Drop(const dataObj: IDataObject;
- grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
- begin
- DragLeave; //Call routine that potentially does tidying up
- Result := S_OK
- end;
-
- procedure TDataFormatListForm.FormCreate(Sender: TObject);
- begin
- //
- end;
-
- procedure TDataFormatListForm.FormDestroy(Sender: TObject);
- begin
- //
- end;
-
- procedure TDataFormatListForm.FormShow(Sender: TObject);
- var
- Res: HResult;
- begin
- Res := RegisterDragDrop(lstDragFmt.Handle, Self);
- OleCheck(Res);
- //OleCheck(RegisterDragDrop(lstDragFmt.Handle, Self));
- //Make sure timer ticks immediately
- if Assigned(Timer.OnTimer) then
- Timer.OnTimer(Timer)
- end;
-
- procedure TDataFormatListForm.FormHide(Sender: TObject);
- begin
- OleCheck(RevokeDragDrop(lstDragFmt.Handle))
- end;
-
- procedure TDataFormatListForm.ListFormats(List: TListItems;
- DataObj: IDataObject);
- var
- EFE: IEnumFormatEtc; //enumeration interface
- FE: TFormatEtc; //Clipboard format, storage medium type etc.
- CElt: Longint; //count of elements returned
- Item: TListItem;
- begin
- OleCheck(dataObj.EnumFormatEtc(DATADIR_GET, EFE));
- List.BeginUpdate;
- try
- List.Clear;
- CElt := -1;
- while CElt <> 0 do
- begin
- OleCheck(EFE.Next(1, FE, @CElt));
- if CElt > 0 then
- begin
- Item := List.Add;
- Item.Caption := ClipFormatToStr(FE.cfFormat);
- Item.SubItems.Add(TyMedToStr(FE.tymed));
- end
- end
- finally
- List.EndUpdate
- end;
- end;
-
- procedure TDataFormatListForm.TimerTimer(Sender: TObject);
- var
- DataObj: IDataObject;
- begin
- if Succeeded(OleGetClipboard(DataObj)) then
- ListFormats(lstClipFmt.Items, DataObj)
- end;
-
-
- initialization
- OleCheck(OleInitialize(nil))
- finalization
- OleUninitialize
- end.
-